﻿using Edu.Http.Extensions;
using Edu.Http.Proxy;
using Infrastructure.Config.Core.External;
using Infrastructure.ExternalService.Core.EduOrder;
using Infrastructure.ExternalService.Core.EduOrder.Request;
using Infrastructure.ExternalService.Core.Responses;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace Infrastructure.ExternalService.Core.Imp.EduOrder
{
    public class EduOrderBaseDataExternal : BaseHttpClient, IEduOrderBaseDataExternal
    {
        private readonly OrderCenterApiOptions _orderingOptions;

        public EduOrderBaseDataExternal(IOptions<OrderCenterApiOptions> options
      , HttpClient client
      , ILoggerFactory loggerFactory)
      : base(client, loggerFactory, options.Value.EduOrderBaseDataOptions.EndPoint)
        {
            _orderingOptions = options.Value ?? throw new HttpProxyException($"{nameof(EduOrderBaseDataExternal)}配置获取失败");

            loggerFactory.CreateLogger<EduOrderBaseDataExternal>();
        }


        public async Task<IsHaveOrderDto> IsHaveOrder(GetOrderByProIdRequest Request)
        {
            var response = await GetAsync<WrapResponse<IsHaveOrderDto>>(_orderingOptions.EduOrderBaseDataOptions.GetIsHaveOrder, Request);

            if (response.Code.Equals(ClientReturnCode.Success) && response.Data != null)
            {
                return response.Data;
            }

            throw new HttpProxyException($"{response.Msg}");
        }

        public async Task<string> GetOrderIdByReceiptNumber(ReceiptNumberDto request)
        {
            var response = await PostAsync<WrapResponse<string>>(_orderingOptions.EduOrderBaseDataOptions.GetOrderIdByReceiptNumber, request);

            if (response.Code.Equals(ClientReturnCode.Success) && response.Data != null)
            {
                return response.Data;
            }

            throw new HttpProxyException($"{response.Msg}");
        }
    }
}
